package com.example.paichaapp;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.Observer;

import com.example.paichaapp.LiveData.MyLiveData;
import com.tencent.mmkv.MMKV;

import org.json.JSONObject;

public class AlarmService extends Service implements LifecycleOwner {

    private int isAlarm = 0;
    private String cause;
    private AlarmBinder alarmBinder = new AlarmBinder();
    MainActivity mactivity;
    LifecycleRegistry lifecycleRegistry = new LifecycleRegistry(this);

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return lifecycleRegistry;
    }

    public void setServe() {
        final Observer<String> observer = new Observer<String>() {
            @SuppressLint("LongLogTag")
            @Override
            public void onChanged(String s) {
                try {
                    JSONObject jsonObject = new JSONObject(s);
                    if (jsonObject.getString("option") != null) {
                        if (jsonObject.getString("option").equals("1")) {
                            cause = jsonObject.getString("cause");
                            isAlarm = 1;


                            MMKV mmkv = MMKV.mmkvWithID("contactPerson");
                            int i = 1;
                            for (; mmkv.decodeString("person" + i) != null; i++) {
                                if (mmkv.decodeString("switch" + i).equals("on")) {
//                                    Intent intent = new Intent();
//                                    intent.setAction(intent.ACTION_SENDTO);
//                                    intent.setData(Uri.parse("smsto:" + mmkv.decodeString("phone" + i)));
//                                    intent.putExtra("sms_body", "警报！排插端口异常！");
//                                    mactivity.startActivity(intent);
                                    System.out.println("sb");
                                    SmsManager sms=SmsManager.getDefault();
                                    sms.sendTextMessage(mmkv.decodeString("phone" + i),null,"警报！排插端口异常！",null,null);
                                }
                            }
                            System.out.println("ssb");
                            AlertDialog.Builder dialog = new AlertDialog.Builder(mactivity);
                            dialog.setTitle("警报");
                            dialog.setMessage("收到端口警报：" + cause);
                            dialog.setCancelable(false);
                            dialog.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                }
                            });
                            dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                }
                            });
                            dialog.show();
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        MyLiveData.getMessageData().observe(this, observer);
    }

    class AlarmBinder extends Binder {

        public int Alarm() {
            return isAlarm;
        }

        public String Cause() {
            return cause;
        }

        public void setActivity(MainActivity activity) {
            mactivity = activity;
        }



    }

    public AlarmService() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME);
        return alarmBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
        setServe();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

//        new Thread(new Runnable() {
//            @Override
//            public void run() {

//            }
//        }).start();
//        Intent intent1 = new Intent(AlarmService.this,MainActivity.class);
//        startActivity(intent1);

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
        return super.onUnbind(intent);
    }
}